package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.view.GlideRoundTransform;

import java.util.List;

/**
 * 质量检查中的添加照片适配器
 */
public class AualityTestAddPicAdapter extends
        RecyclerView.Adapter {
    private Context context;
    private List<String> paths;
    private OnRecyclerViewClickListener onRecyclerViewClickListener;
    private boolean isLocal;

    public interface OnRecyclerViewClickListener extends View.OnClickListener{
        void onItemClick(View v,int position);
    }

    public AualityTestAddPicAdapter(Context context, List<String> paths,OnRecyclerViewClickListener onRecyclerViewClickListener,boolean isLocal) {
        this.context = context;
        this.paths = paths;
        this.onRecyclerViewClickListener=onRecyclerViewClickListener;
        this.isLocal=isLocal;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_addtest_pic, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder) {
            String path=paths.get(position);
            ViewHolder viewHolder = (ViewHolder) holder;
            if(Utils.ADDPIC.equals(path)){
                viewHolder.imageView.setImageResource(R.mipmap.img_add);
                viewHolder.delImageView.setVisibility(View.GONE);

            }else{
//                if(!Utils.isEmpty(path)){
//                    path = path.substring(0, path.lastIndexOf(".")) + "_100x100" + path.substring(path.lastIndexOf("."));
//                }
                if(isLocal){
                    viewHolder.delImageView.setVisibility(View.VISIBLE);
                    Glide.with(context).load(path).transform(new GlideRoundTransform(context, 10)).into(viewHolder.imageView);
                }else{
//                    Glide.with(context).load(Utils.getMinPicture(paths.get(position))).placeholder(R.mipmap.detail_loading).error(R.mipmap.detail_loading).dontAnimate().transform(new GlideRoundTransform(context, 10)).into(viewHolder.imageView);
                    Glide.with(context).load(path).placeholder(R.mipmap.detail_loading).error(R.mipmap.detail_loading).dontAnimate().transform(new GlideRoundTransform(context, 10)).into(viewHolder.imageView);
                }
            }
            viewHolder.imageView.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    onRecyclerViewClickListener.onItemClick(v,position);
                }
            });

            viewHolder.delImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRecyclerViewClickListener.onItemClick(v, position);
                }
            });
        }
    }


    public void setPaths(List<String> paths){
        this.paths=paths;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return paths.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView imageView;
        private final ImageView delImageView;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.item_imageview);
            delImageView = (ImageView) itemView.findViewById(R.id.del_iv);
        }
    }

}
